local M = {}
local t = require('utils.table')
local b = require('utils.buf')

-- from telescrope source
M.get_oldfiles = function()
  local current_buffer = b.get_current()
  local current_file = b.get_current_filename()
  local results = {}

  bufs = vim.split(vim.fn.execute ":buffers! t", "\n")
  t.foreach(bufs,function(buf)
      local match = tonumber(string.match(buf, "%s*(%d+)"))
      if match then
        local file = b.get_filename(match)
        if vim.loop.fs_stat(file) and match ~= current_buffer then
            t.append(results, file)
        end
      end
  end)

  t.foreach(vim.v.oldfiles,function(file)
    local file_stat = vim.loop.fs_stat(file)
    if file_stat and file_stat.type == "file" and not t.contains(results, file) and file ~= current_file then
      t.append(results,file)
    end
  end)

  local cwd = vim.fn.getcwd()
  cwd = cwd .. require("plenary.path").path.sep
  cwd = cwd:gsub([[\]], [[\\]])
  results = t.filter(results,function(file)
    return vim.fn.matchstrpos(file, cwd)[2] ~= -1
  end)

  return results

end

return M
-- EOP
